//fileupdate.c

#include <stdio.h>
#include <string.h>
#include <dirent.h> 
#include <sys/stat.h>
#include "ScreenPrint.h"
#include "USER_CFG.h"
#include "fileupdate.h"

extern unsigned int Cursor_x,Cursor_y;
extern unsigned short GUI_LCM_XMAX,GUI_LCM_YMAX;
 
extern _enum_DMODE G_DispMode;

unsigned int FileSize;
unsigned int TempSize;
unsigned int Pos_Line;

#define NUM_FOLDER 19

char * FileConfig[4] =
{
	"/mnt/sdcard/LIFT/LIFT_0C.csv",
	"/mnt/sdcard/LIFT/LIFT_90C.csv",
	"/mnt/sdcard/LIFT/LIFT_180C.csv",
	"/mnt/sdcard/LIFT/LIFT_270C.csv"
};

char * FileConfig_Flash[4] =
{
	"/mnt/nand1-2/LIFT/LIFT_0C.csv",
	"/mnt/nand1-2/LIFT/LIFT_90C.csv",
	"/mnt/nand1-2/LIFT/LIFT_180C.csv",
	"/mnt/nand1-2/LIFT/LIFT_270C.csv"
};


char * DirInfo_SD_0C[] =
{
//Ò»Œ¶ÎÄŒþŒÐ
	"/mnt/sdcard/LIFT",
	"/mnt/sdcard/LIFT",
	"/mnt/sdcard/LIFT",
//¶þŒ¶ÎÄŒþŒÐ
	"/mnt/sdcard/LIFT/LIFT_0C",
//ÈýŒ¶ÎÄŒþŒÐ
	"/mnt/sdcard/LIFT/LIFT_0C/BKGND",
	"/mnt/sdcard/LIFT/LIFT_0C/SYS",
//ËÄŒ¶ÎÄŒþŒÐ
	
	"/mnt/sdcard/LIFT/LIFT_0C/BKGND/FLR_BSD",
	"/mnt/sdcard/LIFT/LIFT_0C/SYS/FLR", // Â¥²ãÐÅÏ¢
	"/mnt/sdcard/LIFT/LIFT_0C/SYS/DIR", // ŒýÍ·ÐÅÏ¢
	"/mnt/sdcard/LIFT/LIFT_0C/SYS/LFTSTE", // µçÌÝ×ŽÌ¬ÐÅÏ¢
	"/mnt/sdcard/LIFT/LIFT_0C/SYS/DATETME", // Ê±ŒäÐÅÏ¢
	"/mnt/sdcard/LIFT/LIFT_0C/SYS/DAY",     // Ê±ŒäÐÅÏ¢--ÄêÔÂÈÕ

	"/mnt/sdcard/LIFT/LIFT_0C/SYS/DES",     // Ä¿µÄ²ãËØ²Ä
	"/mnt/sdcard/LIFT/FONT",
	"/mnt/sdcard/LIFT/MP3",
	"/mnt/sdcard/LIFT/VIDEO",
	"/mnt/sdcard/LIFT/VIDEO",
	"/mnt/sdcard/LIFT/VIDEO",
	"/mnt/sdcard/LIFT/BOOT"
};

char * DirInfo_SD_90C[] =
{
//Ò»Œ¶ÎÄŒþŒÐ
	"/mnt/sdcard/LIFT",
	"/mnt/sdcard/LIFT",
	"/mnt/sdcard/LIFT",
//¶þŒ¶ÎÄŒþŒÐ
	"/mnt/sdcard/LIFT/LIFT_90C",
//ÈýŒ¶ÎÄŒþŒÐ
	"/mnt/sdcard/LIFT/LIFT_90C/BKGND",
	"/mnt/sdcard/LIFT/LIFT_90C/SYS",
//ËÄŒ¶ÎÄŒþŒÐ
	"/mnt/sdcard/LIFT/LIFT_90C/BKGND/FLR_BSD",
	"/mnt/sdcard/LIFT/LIFT_90C/SYS/FLR", // Â¥²ãÐÅÏ¢
	"/mnt/sdcard/LIFT/LIFT_90C/SYS/DIR", // ŒýÍ·ÐÅÏ¢
	"/mnt/sdcard/LIFT/LIFT_90C/SYS/LFTSTE", // µçÌÝ×ŽÌ¬ÐÅÏ¢
	"/mnt/sdcard/LIFT/LIFT_90C/SYS/DATETME", // Ê±ŒäÐÅÏ¢
	"/mnt/sdcard/LIFT/LIFT_90C/SYS/DAY",     // Ê±ŒäÐÅÏ¢--ÄêÔÂÈÕ

	"/mnt/sdcard/LIFT/LIFT_90C/SYS/DES",     // Ä¿µÄ²ãËØ²Ä
	"/mnt/sdcard/LIFT/FONT",
	"/mnt/sdcard/LIFT/MP3",
	"/mnt/sdcard/LIFT/VIDEO",
	"/mnt/sdcard/LIFT/VIDEO",
	"/mnt/sdcard/LIFT/VIDEO",
	"/mnt/sdcard/LIFT/BOOT"
};

char * DirInfo_SD_180C[] =
{
//Ò»Œ¶ÎÄŒþŒÐ
	"/mnt/sdcard/LIFT",
	"/mnt/sdcard/LIFT",
	"/mnt/sdcard/LIFT",
//¶þŒ¶ÎÄŒþŒÐ
	"/mnt/sdcard/LIFT/LIFT_180C",
//ÈýŒ¶ÎÄŒþŒÐ
	"/mnt/sdcard/LIFT/LIFT_180C/BKGND",
	"/mnt/sdcard/LIFT/LIFT_180C/SYS",
//ËÄŒ¶ÎÄŒþŒÐ

	"/mnt/sdcard/LIFT/LIFT_180C/BKGND/FLR_BSD",
	"/mnt/sdcard/LIFT/LIFT_180C/SYS/FLR", // Â¥²ãÐÅÏ¢
	"/mnt/sdcard/LIFT/LIFT_180C/SYS/DIR", // ŒýÍ·ÐÅÏ¢
	"/mnt/sdcard/LIFT/LIFT_180C/SYS/LFTSTE", // µçÌÝ×ŽÌ¬ÐÅÏ¢
	"/mnt/sdcard/LIFT/LIFT_180C/SYS/DATETME", // Ê±ŒäÐÅÏ¢
	"/mnt/sdcard/LIFT/LIFT_180C/SYS/DAY",     // Ê±ŒäÐÅÏ¢--ÄêÔÂÈÕ
	
	"/mnt/sdcard/LIFT/LIFT_180C/SYS/DES",     // Ä¿µÄ²ãËØ²Ä
	"/mnt/sdcard/LIFT/FONT",
	"/mnt/sdcard/LIFT/MP3",
	"/mnt/sdcard/LIFT/VIDEO",
	"/mnt/sdcard/LIFT/VIDEO",
	"/mnt/sdcard/LIFT/VIDEO",
	"/mnt/sdcard/LIFT/BOOT"
};

char * DirInfo_SD_270C[] =
{
//Ò»Œ¶ÎÄŒþŒÐ
	"/mnt/sdcard/LIFT",
	"/mnt/sdcard/LIFT",
	"/mnt/sdcard/LIFT",
//¶þŒ¶ÎÄŒþŒÐ
	"/mnt/sdcard/LIFT/LIFT_270C",
//ÈýŒ¶ÎÄŒþŒÐ
	"/mnt/sdcard/LIFT/LIFT_270C/BKGND",
	"/mnt/sdcard/LIFT/LIFT_270C/SYS",
//ËÄŒ¶ÎÄŒþŒÐ

	"/mnt/sdcard/LIFT/LIFT_270C/BKGND/FLR_BSD",
	"/mnt/sdcard/LIFT/LIFT_270C/SYS/FLR", // Â¥²ãÐÅÏ¢
	"/mnt/sdcard/LIFT/LIFT_270C/SYS/DIR", // ŒýÍ·ÐÅÏ¢
	"/mnt/sdcard/LIFT/LIFT_270C/SYS/LFTSTE", // µçÌÝ×ŽÌ¬ÐÅÏ¢
	"/mnt/sdcard/LIFT/LIFT_270C/SYS/DATETME", // Ê±ŒäÐÅÏ¢
	"/mnt/sdcard/LIFT/LIFT_270C/SYS/DAY",     // Ê±ŒäÐÅÏ¢--ÄêÔÂÈÕ

	"/mnt/sdcard/LIFT/LIFT_270C/SYS/DES",     // Ä¿µÄ²ãËØ²Ä
	"/mnt/sdcard/LIFT/FONT",
	"/mnt/sdcard/LIFT/MP3",
	"/mnt/sdcard/LIFT/VIDEO",
	"/mnt/sdcard/LIFT/VIDEO",
	"/mnt/sdcard/LIFT/VIDEO",
	"/mnt/sdcard/LIFT/BOOT"
};

char * DirInfo_SpiFlash_0C[] =
{
//Ò»Œ¶ÎÄŒþŒÐ
	"/mnt/nand1-2",
	"/mnt/nand1-2/LIFT",
	"/mnt/nand1-2/LIFT",
//¶þŒ¶ÎÄŒþŒÐ
	"/mnt/nand1-2/LIFT/LIFT_0C",
//ÈýŒ¶ÎÄŒþŒÐ
	"/mnt/nand1-2/LIFT/LIFT_0C/BKGND",
	"/mnt/nand1-2/LIFT/LIFT_0C/SYS",
//ËÄŒ¶ÎÄŒþŒÐ

	"/mnt/nand1-2/LIFT/LIFT_0C/BKGND/FLR_BSD",
	"/mnt/nand1-2/LIFT/LIFT_0C/SYS/FLR", // Â¥²ãÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_0C/SYS/DIR", // ŒýÍ·ÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_0C/SYS/LFTSTE", // µçÌÝ×ŽÌ¬ÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_0C/SYS/DATETME", // Ê±ŒäÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_0C/SYS/DAY",     // Ê±ŒäÐÅÏ¢--ÄêÔÂÈÕ

	"/mnt/nand1-2/LIFT/LIFT_0C/SYS/DES",     // Ä¿µÄ²ãËØ²Ä
	"/mnt/nand1-2/LIFT/FONT",
	"/mnt/nand1-2/LIFT/MP3",
	"/mnt/nand1-2/LIFT/VIDEO",
	"/mnt/nand1-2/LIFT/VIDEO",
	"/mnt/nand1-2/LIFT/VIDEO",
	"/mnt/nand1-1"
};

char * DirInfo_SpiFlash_90C[] =
{
//Ò»Œ¶ÎÄŒþŒÐ
	"/mnt/nand1-2",
	"/mnt/nand1-2/LIFT",
	"/mnt/nand1-2/LIFT",
//¶þŒ¶ÎÄŒþŒÐ
	"/mnt/nand1-2/LIFT/LIFT_90C",
//ÈýŒ¶ÎÄŒþŒÐ
	"/mnt/nand1-2/LIFT/LIFT_90C/BKGND",
	"/mnt/nand1-2/LIFT/LIFT_90C/SYS",
//ËÄŒ¶ÎÄŒþŒÐ
	"/mnt/nand1-2/LIFT/LIFT_90C/BKGND/FLR_BSD",
	"/mnt/nand1-2/LIFT/LIFT_90C/SYS/FLR", // Â¥²ãÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_90C/SYS/DIR", // ŒýÍ·ÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_90C/SYS/LFTSTE", // µçÌÝ×ŽÌ¬ÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_90C/SYS/DATETME", // Ê±ŒäÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_90C/SYS/DAY",     // Ê±ŒäÐÅÏ¢--ÄêÔÂÈÕ

	"/mnt/nand1-2/LIFT/LIFT_90C/SYS/DES",     // Ä¿µÄ²ãËØ²Ä
	"/mnt/nand1-2/LIFT/FONT",
	"/mnt/nand1-2/LIFT/MP3",
	"/mnt/nand1-2/LIFT/VIDEO",
	"/mnt/nand1-2/LIFT/VIDEO",
	"/mnt/nand1-2/LIFT/VIDEO",
	"/mnt/nand1-1"
};

char * DirInfo_SpiFlash_180C[] =
{
//Ò»Œ¶ÎÄŒþŒÐ
	"/mnt/nand1-2",
	"/mnt/nand1-2/LIFT",
	"/mnt/nand1-2/LIFT",
//¶þŒ¶ÎÄŒþŒÐ
	"/mnt/nand1-2/LIFT/LIFT_180C",
//ÈýŒ¶ÎÄŒþŒÐ
	"/mnt/nand1-2/LIFT/LIFT_180C/BKGND",
	"/mnt/nand1-2/LIFT/LIFT_180C/SYS",
//ËÄŒ¶ÎÄŒþŒÐ

	"/mnt/nand1-2/LIFT/LIFT_180C/BKGND/FLR_BSD",
	"/mnt/nand1-2/LIFT/LIFT_180C/SYS/FLR", // Â¥²ãÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_180C/SYS/DIR", // ŒýÍ·ÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_180C/SYS/LFTSTE", // µçÌÝ×ŽÌ¬ÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_180C/SYS/DATETME", // Ê±ŒäÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_180C/SYS/DAY",     // Ê±ŒäÐÅÏ¢--ÄêÔÂÈÕ

	"/mnt/nand1-2/LIFT/LIFT_180C/SYS/DES", 	  // Ä¿µÄ²ãËØ²Ä
	"/mnt/nand1-2/LIFT/FONT",
	"/mnt/nand1-2/LIFT/MP3",
	"/mnt/nand1-2/LIFT/VIDEO",
	"/mnt/nand1-2/LIFT/VIDEO",
	"/mnt/nand1-2/LIFT/VIDEO",
	"/mnt/nand1-1"
};

char * DirInfo_SpiFlash_270C[] =
{
//Ò»Œ¶ÎÄŒþŒÐ
	"/mnt/nand1-2",
	"/mnt/nand1-2/LIFT",
	"/mnt/nand1-2/LIFT",
//¶þŒ¶ÎÄŒþŒÐ
	"/mnt/nand1-2/LIFT/LIFT_270C",
	"/mnt/nand1-2/LIFT/LIFT_270C/BKGND",
	"/mnt/nand1-2/LIFT/LIFT_270C/SYS",
//ËÄŒ¶ÎÄŒþŒÐ
	
	"/mnt/nand1-2/LIFT/LIFT_270C/BKGND/FLR_BSD",
	"/mnt/nand1-2/LIFT/LIFT_270C/SYS/FLR", // Â¥²ãÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_270C/SYS/DIR", // ŒýÍ·ÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_270C/SYS/LFTSTE", // µçÌÝ×ŽÌ¬ÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_270C/SYS/DATETME", // Ê±ŒäÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_270C/SYS/DAY",     // Ê±ŒäÐÅÏ¢--ÄêÔÂÈÕ
	"/mnt/nand1-2/LIFT/LIFT_270C/SYS/DES",     // Ä¿µÄ²ãËØ²Ä

	"/mnt/nand1-2/LIFT/FONT",
	"/mnt/nand1-2/LIFT/MP3",                                                                                                                             
	"/mnt/nand1-2/LIFT/VIDEO",
	"/mnt/nand1-2/LIFT/VIDEO",
	"/mnt/nand1-2/LIFT/VIDEO",
	"/mnt/nand1-1"
};

char * HouzhuiCopy[] =
{
	"bin",
	"txt",
	"csv",
	
	"bmp",
	"bmp",
	"bmp",
	
	"bmp",
	"bmp",
	"bmp",
	"bmp",
	"bmp",
	"bmp",
	"bmp",
	
	"fon",
	"mp3",
	"jpg",
	"bmp",
	"mp3",
	"bin"
};

char ** pDirInfo_SD[4] =
{
	DirInfo_SD_0C,
	DirInfo_SD_90C,
	DirInfo_SD_180C,
	DirInfo_SD_270C,
};

char ** pDirInfo_SpiFlash[4] =
{
	DirInfo_SpiFlash_0C,
	DirInfo_SpiFlash_90C,
	DirInfo_SpiFlash_180C,
	DirInfo_SpiFlash_270C,
};

void ChangeName2Low(char * filename)
{
	while((*filename) != '\0')
	{
		if(((*filename) >= 'A') && ((*filename) <= 'Z'))
		{
			*filename = (*filename) + ('a' - 'A');
		}

		filename++;
	}
}

//Œì²éÊÇ·ñŽæÔÚÖž¶šÎÄŒþ£¬ŽæÔÚ·µ»ØÖµÎª1£¬·ñÔòÎª0
unsigned int CheckFileExist(char * filename)
{
	FILE * pic_ptr = fopen (filename, "r");
	
	if(pic_ptr==NULL)
		return 0;
	else
	{
		fclose(pic_ptr);
		return 1;
	}
}

void Display_Print_Init()
{	
	Cursor_x = 0;
	Cursor_y = 0;
}

void Display_Print(char *s)
{
	int s_len = strlen(s)*8;

	if( (G_DispMode == dmode_0C) || (G_DispMode == dmode_180C) )
	{
		Cursor_x = (GUI_LCM_XMAX-s_len)/2;	
	}
	else if( (G_DispMode == dmode_90C) || (G_DispMode == dmode_270C) )
	{
		Cursor_x = (GUI_LCM_YMAX-s_len)/2;	
	}
	else
	{
		Cursor_x = (GUI_LCM_XMAX-s_len)/2;
	}
	PrintScreen(s);
	PrintScreen("\r\n");
}

void Display_Date_Time(char *s)
{
	int s_len = strlen(s)*8;

	if( (G_DispMode == dmode_0C) || (G_DispMode == dmode_180C) )
	{
		Cursor_x = (GUI_LCM_XMAX-s_len)/2;
		//Cursor_y = (GUI_LCM_YMAX-16)/2;;
	
	}
	else if( (G_DispMode == dmode_90C) || (G_DispMode == dmode_270C) )
	{
		Cursor_x = (GUI_LCM_YMAX-s_len)/2;
		//Cursor_y = (GUI_LCM_XMAX-16)/2;
	
	}
	else
	{
		Cursor_x = (GUI_LCM_XMAX-s_len)/2;
		//Cursor_y = (GUI_LCM_YMAX-16)/2;
	}
	PrintScreen(s);
	PrintScreen("\r\n");
}
//extern int LIFT_UPDATE;
void ProcessSDStatus(void)
{
	unsigned char result;
	unsigned char result1;

	unsigned int time;
	char buf[32];
	unsigned int temp;	
	
	system("umount /mnt/appimg");
	
	system("cp /mnt/sdcard/LIFT/BOOT/boot_exec /mnt/nand1-1/boot_exec -f");
	system("cp /mnt/sdcard/LIFT/BOOT/boot_script /mnt/nand1-1/boot_script -f");
	system("cp /mnt/sdcard/LIFT/BOOT/v4a.rbf /mnt/nand1-1/v4a.rbf -f");
	system("cp /mnt/sdcard/LIFT/BOOT/server_ip /mnt/nand1-2/server_ip -f");
	
	result = CheckFileExist("/mnt/sdcard/LIFT/mkfsflash.exec");
	result1 = CheckFileExist("/mnt/sdcard/LIFT/formatflash.exec");
	if(result || result1)
	{
		printf("Formating Flash...");
		system("rm -rf /mnt/nand1-2/LIFT");
		sleep(1);			
	}

	if( CheckFileExist("/mnt/sdcard/LIFT/flrmapdel.exec") == 1)
	{
		remove("/mnt/nand1-2/LIFT/flrmap.csv");
		Display_Print("Delete flrmap.csv File...");
		sleep(1);
	}
	
	if( CheckFileExist("/mnt/sdcard/LIFT/videodel.exec") == 1)
	{
		system("rm -rf /mnt/nand1-2/LIFT/VIDEO");
		Display_Print("Delete VIDEO Folder...");
		sleep(1);
	}
	
	if( CheckFileExist("/mnt/sdcard/LIFT/muxdel.exec") == 1)
	{
		system("rm -rf /mnt/nand1-2/mux.bin");
		Display_Print("Delete mux.bin file...");
		sleep(1);
	}
	
	int res = CopySdFileToFlash();
		
	Display_Print("\r\n\r\n\r\n");
	
	system("sync");
	sleep(5);
	//if(LIFT_UPDATE)
	//{
	//	Display_Print("Firmware Update OK");
	//	return;
	//}
	system("umount /mnt/sdcard");
	if( res == 0)
	{
		Display_Print("Firmware Update OK");
		Display_Print("Please Remove USB Disk");
	}	
	else
	{
		Display_Print("Firmware Update Failed");
		Display_Print("Please Update Again");	
	}
	
}

unsigned int GetFileSize(char * srcpath,char *filename)
{
	char temppath[64];
	struct stat temp;

	sprintf(temppath,srcpath);
	strcat(temppath,"/");
	strcat(temppath,filename);

	//方法二：fseek , ftell;

	if(0==stat(temppath, &temp))
	return temp.st_size;
	else
	return 0;

}

unsigned int GetFolderSize(char* scan_path,char *scan_houzhui)
{
	DIR *dirptr = NULL;
	struct dirent *entry;
	unsigned int size;
	char *fnhouzhui;
	
	char fnhouzhui_bak[8];

	size = 0;

	if((dirptr = opendir(scan_path)) == NULL)
	{
		printf("open dir fialed!!!\r\n");
		return 0;
	}
	else
	{
		while((entry = readdir(dirptr))!=NULL)
		{

			if(entry-> d_type!= DT_DIR)
			{
				//printf("%s\n",entry-> d_name);
				fnhouzhui = strchr(entry-> d_name,'.');                      // ÈôÎªÎÄŒþ£¬¶šÎ»ºó×ºÃû
	    	  	if(fnhouzhui==NULL)
					continue;
	    	  		fnhouzhui++;
				memset(fnhouzhui_bak,0,8);
				strcat(fnhouzhui_bak,fnhouzhui);
	    	  	ChangeName2Low(fnhouzhui_bak);
				if(!strcmp(fnhouzhui_bak,scan_houzhui))
				 	size += GetFileSize(scan_path,entry->d_name);
			}

		}
		closedir(dirptr);
	}
	return size;
}

unsigned int CopyFile(char * srcpath,char * despath,char *filename)
{
	DIR *dirptr = NULL;
	FILE * fd_ptr1;
	FILE * fd_ptr2;
	size_t res1,res2;
	size_t fsize,tempsize;
	struct dirent *entry;
	char temppath1[64];
	char temppath2[64];
	char buffer[4096];

	int temp_x;
	int temp;
	int x,y;

	sprintf(temppath1,srcpath);
	sprintf(temppath2,despath);
	strcat(temppath1,"/");
	strcat(temppath2,"/");
	strcat(temppath1,filename);
	strcat(temppath2,filename);

	fsize = GetFileSize(srcpath,filename);
	tempsize = 0;
	fd_ptr1 = fopen(temppath1,"r");
	if(fd_ptr1 != NULL)
	{
		fd_ptr2 = fopen(temppath2,"w");
		//printf("%s %s\r\n",temppath1,temppath2);
		if(fd_ptr2 != NULL)
		{
			//printf("%s %s\r\n",temppath1,temppath2);
			while(tempsize < fsize)//每次复制4096个字节,直到整个文件完全复制成功
			{
				res1 = fread(buffer,1,4096,fd_ptr1);
				res2 = fwrite (buffer, 1, res1, fd_ptr2);
				tempsize += res2;
				TempSize += res2;
				if((G_DispMode == dmode_0C) || (G_DispMode == dmode_180C))//获得进度条的长度
				{
					temp_x = GUI_LCM_XMAX / 2;
				}
				else
				{
					temp_x = GUI_LCM_YMAX  / 2;
				}

				temp = FileSize / temp_x;//单位长度进度条代表的文件大小

				while(TempSize >= temp)
				{
					TempSize -= temp;
					if((G_DispMode == dmode_0C) || (G_DispMode == dmode_180C))
					{
						temp_x = GUI_LCM_XMAX / 2;

						x = (GUI_LCM_XMAX - temp_x)/2;
						y = (GUI_LCM_YMAX - 32)/2;
						lcd_vline(x+Pos_Line,y,32,0xFFFF);

						if(G_DispMode == dmode_0C)
						{
							Pos_Line++;
						}
						else
						{
							Pos_Line--;
						}
					}
					else
					{
						temp_x = GUI_LCM_YMAX  / 2;

						x = (GUI_LCM_XMAX - 32)/2;
						y = (GUI_LCM_YMAX - temp_x)/2;
						lcd_hline(x,y+Pos_Line,32,0xFFFF);

						if(G_DispMode == dmode_90C)
						{
							Pos_Line++;
						}
						else
						{
							Pos_Line--;
						}
					}

				}
				if(res2 < res1)		//容量不足
				{
					fclose(fd_ptr2);
					fclose(fd_ptr1);
					return 0xFF;
				}
			}
			fclose(fd_ptr2);
		}

		fclose(fd_ptr1);
	}

	return 0;
}


unsigned char CopyFolder(char* scan_path,char* save_path,char *scan_houzhui)
{
	DIR *dirptr = NULL;
	struct dirent *entry;
	unsigned int result;
	char * fnhouzhui;
	char fnhouzhui_bak[8];

	if((dirptr = opendir(scan_path)) == NULL)
	{
		printf("open dir fialed!!!\r\n");
		return 1;
	}
	else
	{
		while((entry = readdir(dirptr))!=NULL)
		{

			if(entry-> d_type!= DT_DIR)
			{
				fnhouzhui = strchr(entry-> d_name,'.');                      // ÈôÎªÎÄŒþ£¬¶šÎ»ºó×ºÃû
	    	  	if(fnhouzhui==NULL)
					continue;	
	    	  		fnhouzhui++;
				memset(fnhouzhui_bak,0,8);
				strcat(fnhouzhui_bak,fnhouzhui);
	    	  	ChangeName2Low(fnhouzhui_bak);
				if(!strcmp(fnhouzhui_bak,scan_houzhui))
					result = CopyFile(scan_path,save_path,entry-> d_name);

				if(result == 0xFF)
				{
					closedir(dirptr);
					return 0xFF; 
				}
			}

		}
		closedir(dirptr);
	}

	return 0;
}

unsigned char CopySdFileToFlash(void)
{
	int temp_x=0;
	unsigned int i;
	unsigned int config;
	char ** folder_spi;
	char ** folder_sd;

	unsigned int result;

	char buf[256];

	unsigned int x,y;
	unsigned int temp;

	//žŽÖÆžùÄ¿ÂŒÏÂÎÄŒþ
	DIR *dirptr = NULL;
	config = 0;

	if(1)
	{
		
		//Cursor_x = GUI_LCM_XMAX + GUI_LCM_YMAX;
		//Cursor_y = GUI_LCM_XMAX + GUI_LCM_YMAX;

		Display_Print("Updating File,Please Wait...");

		if((G_DispMode == dmode_0C) || (G_DispMode == dmode_180C))
		{
			temp = GUI_LCM_XMAX / 2;

			x = (GUI_LCM_XMAX - temp)/2;
			y = (GUI_LCM_YMAX - 32)/2;

			Cursor_x = 0;
			Cursor_y = (GUI_LCM_YMAX - 32) /2;

			lcd_hline(x,y,temp,0xFFFF);
			lcd_hline(x,y+32,temp,0xFFFF);
			lcd_vline(x,y,32,0xFFFF);
			lcd_vline(x+temp,y,32,0xFFFF);
		}
		else
		{
			temp = GUI_LCM_YMAX  / 2;

			x = (GUI_LCM_XMAX - 32)/2;
			y = (GUI_LCM_YMAX - temp)/2;

			Cursor_x = 0;
			Cursor_y = (GUI_LCM_XMAX - 32) /2;

			lcd_vline(x,y,temp,0xFFFF);
			lcd_vline(x+32,y,temp,0xFFFF);
			lcd_hline(x,y,32,0xFFFF);
			lcd_hline(x,y+temp,32,0xFFFF);
		}

		i = 0;
		
		while(1)
		{
			if( access(FileConfig[i], 0) == 0)
			{
				config = i;
				break;
			}
			i++;
			if(i>3)	
			{
			i = 0;
			break;
			}
		}
		
		config = i;
		printf("config = %d\r\n",config);
		
		folder_spi = pDirInfo_SpiFlash[config];
		folder_sd = pDirInfo_SD[config];
		Display_Print("\r\n\r\n\r\n");
		
		//ŽŽœšËùÓÐÐèÒªµÄÎÄŒþŒÐ
		if(i<4)
		{
			i = 0;
			printf("mkdir start\r\n");
			while(1)
			{
				printf("%s\r\n",*folder_spi);
				result = mkdir(*folder_spi,0777);
				folder_spi++;
				i++;
				if(i == NUM_FOLDER-1) break;
			}
			system("sync");
			sleep(1);
				
			printf("mkdir end\r\n");
			//ŒÆËãÐèÒª¿œ±ŽµÄÎÄŒþ×ÜÈÝÁ¿
			folder_spi = pDirInfo_SpiFlash[config];
			folder_sd = pDirInfo_SD[config];
			i= 0;
			FileSize = 0;
			TempSize = 0;
			while(1)
			{
				printf("%s\r\n",*folder_sd);
				FileSize += GetFolderSize(*folder_sd,HouzhuiCopy[i]);
				folder_sd++;
				i++;
				if(i == NUM_FOLDER) break;
			}
			printf("FileSize=%d\r\n",FileSize);
						
			//žŽÖÆÎÄŒþŒÐÏÂÏàÓŠºó×ºµÄÎÄŒþ
			folder_spi = pDirInfo_SpiFlash[config];
			folder_sd = pDirInfo_SD[config];
			i= 0;
			
			if(G_DispMode == dmode_0C)
			{
				Pos_Line = 0;
			}
			else if(G_DispMode == dmode_90C)
			{
				Pos_Line = 0;
			}
			else if(G_DispMode == dmode_180C)
			{
				Pos_Line = GUI_LCM_XMAX / 2;
			}
			else if(G_DispMode == dmode_270C)
			{
				Pos_Line = GUI_LCM_YMAX  / 2;
			}
			
			if((G_DispMode == dmode_0C) || (G_DispMode == dmode_180C))//获得进度条的长度
			{
				temp_x = GUI_LCM_XMAX / 2;
			}
			else
			{
				temp_x = GUI_LCM_YMAX  / 2;
			}
			if(FileSize < temp_x)//单位长度进度条代表的文件大小
			{
				system("cp /mnt/sdcard/LIFT/*.csv /mnt/nand1-2/LIFT/ -f");
				system("cp /mnt/sdcard/LIFT/ad.txt /mnt/nand1-2/LIFT/ad.txt -f");
				while(temp_x>0)
				{
					temp_x--;
					if((G_DispMode == dmode_0C) || (G_DispMode == dmode_180C))
					{
						x = GUI_LCM_XMAX/4;
						y = (GUI_LCM_YMAX - 32)/2;
						lcd_vline(x+Pos_Line,y,32,0xFFFF);

						if(G_DispMode == dmode_0C)
						{
							Pos_Line++;
						}
						else
						{
							Pos_Line--;
						}
					}
					else
					{
						temp_x = GUI_LCM_YMAX  / 2;

						x = (GUI_LCM_XMAX - 32)/2;
						y = GUI_LCM_YMAX/4;
						
						lcd_hline(x,y+Pos_Line,32,0xFFFF);

						if(G_DispMode == dmode_90C)
						{
							Pos_Line++;
						}
						else
						{
							Pos_Line--;
						}
					}

				}	
				
				return 0;
			}

			while(1)
			{
				printf("copy %s start\r\n",*folder_spi);
				result = CopyFolder(*folder_sd,*folder_spi,HouzhuiCopy[i]);
				if(result == 0xFF)
				{
					Display_Print("\r\n\r\n\r\n");
					Display_Print("*****NO enough space");
					return 0xFF;		//ŽæŽ¢Æ÷ÈÝÁ¿²»×ã
				}
				folder_sd++;
				folder_spi++;
				i++;
				if(i == NUM_FOLDER) break;
			}
		}
	}
	
	return 0;
}


